package com.j2203.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class EmailUtils {
    @Autowired
    JavaMailSender javaMailSender;//邮件发送对象
    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private StringTools stringTools;



    public String sendMail(String toMail) throws MessagingException {
        String code=stringTools.getCode(6);
        //        发送邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //创建发送包含特殊类型的内容
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setFrom(from);//发送者
        mimeMessageHelper.setTo(toMail);//接收者
        mimeMessageHelper.setSubject("邮箱验证");
        mimeMessageHelper.setText("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "\n" +
                "<head>\n" +
                "  <meta charset=\"UTF-8\">\n" +
                "  <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
                "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "  <title>验证邮件</title>\n" +
                "\n" +
                "  <style>\n" +
                "    body,html,div,ul,li,button,p,img,h1,h2,h3,h4,h5,h6 {\n" +
                "      margin: 0;\n" +
                "      padding: 0;\n" +
                "    }\n" +
                "\n" +
                "    body,html {\n" +
                "      background: #fff;\n" +
                "      line-height: 1.8;\n" +
                "    }\n" +
                "\n" +
                "    h1,h2,h3,h4,h5,h6 {\n" +
                "      line-height: 1.8;\n" +
                "    }\n" +
                "\n" +
                "    .email_warp {\n" +
                "      height: 100vh;\n" +
                "      min-height: 500px;\n" +
                "      font-size: 14px;\n" +
                "      color: #212121;\n" +
                "      display: flex;\n" +
                "      /* align-items: center; */\n" +
                "      justify-content: center;\n" +
                "    }\n" +
                "\n" +
                "    .logo {\n" +
                "      margin: 3em auto;\n" +
                "      width: 200px;\n" +
                "      height: 60px;\n" +
                "    }\n" +
                "\n" +
                "    h1.email-title {\n" +
                "      font-size: 26px;\n" +
                "      font-weight: 500;\n" +
                "      margin-bottom: 15px;\n" +
                "      color: #252525;\n" +
                "    }\n" +
                "\n" +
                "    a.links_btn {\n" +
                "      border: 0;\n" +
                "      background: #4C84FF;\n" +
                "      color: #fff;\n" +
                "      width: 100%;\n" +
                "      height: 50px;\n" +
                "      line-height: 50px;\n" +
                "      font-size: 16px;\n" +
                "      margin: 40px auto;\n" +
                "      box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.15);\n" +
                "      border-radius: 4px;\n" +
                "      outline: none;\n" +
                "      cursor: pointer;\n" +
                "      transition: all 0.3s;\n" +
                "      text-align: center;\n" +
                "      display: block;\n" +
                "      text-decoration: none;\n" +
                "    }\n" +
                "\n" +
                "    .warm_tips {\n" +
                "      color: #757575;\n" +
                "      background: #f7f7f7;\n" +
                "      padding: 20px;\n" +
                "    }\n" +
                "\n" +
                "    .warm_tips .desc {\n" +
                "      margin-bottom: 20px;\n" +
                "    }\n" +
                "\n" +
                "    .qr_warp {\n" +
                "      max-width: 140px;\n" +
                "      margin: 20px auto;\n" +
                "    }\n" +
                "\n" +
                "    .qr_warp img {\n" +
                "      max-width: 100%;\n" +
                "      max-height: 100%;\n" +
                "    }\n" +
                "\n" +
                "    .email-footer {\n" +
                "      margin-top: 2em;\n" +
                "    }\n" +
                "\n" +
                "    #reset-password-email {\n" +
                "      max-width: 500px;\n" +
                "    }\n" +
                "    #reset-password-email .accout_email {\n" +
                "      color: #4C84FF;\n" +
                "      display: block;\n" +
                "      margin-bottom: 20px;\n" +
                "      font-size: 30px;\n" +
                "      text-align: center;\n" +
                "    }\n" +
                "  </style>\n" +
                "</head>\n" +
                "\n" +
                "<body>\n" +
                "  <section class=\"email_warp\">\n" +
                "    <div id=\"reset-password-email\">\n" +
                "      <div class=\"logo\">\n" +
                "        <img src=\"https://logos-world.net/wp-content/uploads/2020/10/Steam-Logo-2002-present.png\" alt=\"logo\" width=\"200px\" height=\"90px\">\n" +
                "      </div>\n" +
                "\n" +
                "      <h1 class=\"email-title\">\n" +
                "        尊敬的<span>"+toMail+"</span>您好：\n" +
                "      </h1>\n" +
                "      <p>您正在为steam注册账号,验证码为：</p>\n" +
                "      <b class=\"accout_email\">"+code+"</b>\n" +
                "\n" +
                "      <p>请注意，如果这不是您本人的操作，请忽略并关闭此邮件。</p>\n" +
                "      \n" +
                "     \n" +
                "\n" +
                "      <div class=\"warm_tips\">\n" +
                "        <div class=\"desc\">\n" +
                "          为安全起见，且仅在24小时内有效，请您尽快完成操作。\n" +
                "        </div>\n" +
                "\n" +
                "        <p>如有任何疑问或无法完成注册，请通过如下方式与我们联系：</p>\n" +
                "        <p>邮箱：3173577838@qq.com</p>\n" +
                "        <p>QQ助理：3173577838</p>\n" +
                "\n" +
                "       \n" +
                "        <p>本邮件由系统自动发送，请勿回复。</p>\n" +
                "      </div>\n" +
                "\n" +
                "      <div class=\"email-footer\">\n" +
                "        <p>您的智能项目助理</p>\n" +
                "        <p>J2203第五组</p>\n" +
                "      </div>\n" +
                "    </div>\n" +
                "  </section>\n" +
                "</body>\n" +
                "\n" +
                "</html>",true);

        javaMailSender.send(mimeMessage);
        return code;
    }
}
